package com.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.product.enmu.HttpStatusEnum;
import com.product.entity.AssemblyLine;
import com.product.handler.ParameterValidationException;
import com.product.result.ApiResult;
import com.product.service.IAssemblyLineService;
import com.product.service.IInspectorCordsService;
import com.product.to.AddInspectorCordTo;
import com.product.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "检测记录控制器")
@RestController
@RequestMapping("/inspectorCords")
public class InspectorCordsController {
    @Autowired
    private IInspectorCordsService inspectorCordsService;
    @Autowired
    private IAssemblyLineService assemblyLineService;

    @Parameters({
            @Parameter(name = "size", description = "每页条数，默认10条"),
            @Parameter(name = "current", description = "分页页数，默认第一页"),
            @Parameter(name = "lineId", description = "流水线id"),
            @Parameter(name = "orderItemId", description = "订单项id"),
            @Parameter(name = "timeOrderBy", description = "按照最后检测时间顺序排序,0->升序,1->降序"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "获取所有检测记录")
    @GetMapping("/all")
    private ApiResult<PageUtils> getAllInspectorCords(@RequestParam(name = "size", defaultValue = "10") Integer size,
                                             @RequestParam(name = "current", defaultValue = "1") Integer current,
                                             @RequestParam(name = "lineId", required = false) Integer lineId,
                                             @RequestParam(name = "orderItemId", required = false) Integer orderItemId,
                                             @RequestParam(name = "timeOrderBy", defaultValue = "1") Integer timeOrderBy) {
        return ApiResult.ok(inspectorCordsService.getAllInspectorCords(size, current, lineId, orderItemId, timeOrderBy));
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "添加检测")
    @PostMapping("/addDetect")
    private ApiResult<Boolean> addInspectorCords(@Valid @RequestBody AddInspectorCordTo inspectorCords, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        if (assemblyLineService.list(new LambdaQueryWrapper<AssemblyLine>().eq(AssemblyLine::getLineId, inspectorCords.getLineId())).isEmpty()) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "流水线不存在");
        }
        inspectorCordsService.addInspectorCords(inspectorCords);
        return ApiResult.ok();
    }

    @Parameters({
            @Parameter(name = "inspectorCordsId", description = "检测记录id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "删除检测记录")
    @PostMapping("/delete")
    private ApiResult<Boolean> deleteInspectorCord(@RequestParam(name = "inspectorCordsId") Integer inspectorCordsId) {
        inspectorCordsService.deleteInspectorCords(inspectorCordsId);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "批量删除检测记录")
    @PostMapping("/deletes")
    private ApiResult<Boolean> deleteInspectorCords(@RequestBody List<Integer> inspectorCordsIds) {
        inspectorCordsService.deleteInspectorCordss(inspectorCordsIds);
        return ApiResult.ok();
    }
}
